from configs.global_config import global_config
from controllers.base_controller import BaseController
from utils.api_response_utils import create_success_response


class SystemController(BaseController):
    """系统控制器，处理系统相关请求"""
    
    def __init__(self, import_name):
        super().__init__("system", import_name, url_prefix="")
    
    def register_routes(self):
        """注册路由"""
        
        @self.route('/reload_cors', methods=['POST'])
        @self.swagger_from('swagger_docs/reload_cors.yml')
        def reload_cors():
            """重新加载CORS配置端点"""
            try:
                if global_config.reload():
                    # 提供默认值 ["*"] 而非空数组 []，避免阻止所有跨域请求
                    new_origins = global_config.get("cors.origins", ["*"])
                    # 更新应用配置
                    from flask_server import app
                    app.config['CORS_ORIGINS'] = new_origins
                    return create_success_response("CORS配置已更新", {
                        "new_origins": new_origins
                    })
            except Exception as e:
                from utils.api_response_utils import create_error_response
                return create_error_response(500, f"刷新CORS配置失败: {str(e)}")
                
        @self.route('/health', methods=['GET'])
        @self.swagger_from('swagger_docs/health.yml')
        def health_check():
            """健康检查端点，用于Nacos服务发现"""
            return create_success_response("服务正常运行", {
                "status": "UP",
                "services": {
                    "face_feature": "UP",
                    "speech_recognition": "UP"
                },
                "version": "1.0.0"
            })